package com.dp.mallchat.common.user.service.impl;

import com.dp.mallchat.common.common.utils.AssertUtil;
import com.dp.mallchat.common.user.domain.user.enums.OssSceneEnum;
import com.dp.mallchat.common.user.domain.user.req.UploadUrlReq;
import com.dp.mallchat.common.user.service.OssService;
import com.dp.mallchat.oss.MinIOTemplate;
import com.dp.mallchat.oss.domain.OssReq;
import com.dp.mallchat.oss.domain.OssResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: dupeng
 * @CreateTime: 2024-07-17  20:22
 * @Description: 对象存储服务
 */
@Service
@Slf4j
public class OssServiceImpl implements OssService {

    @Resource
    private MinIOTemplate minIOTemplate;

    /**
     * 获取临时上传地址
     * @param uploadUrlReq 对象存储请求
     * @return
     */
    @Override
    public OssResp getUploadUrl(Long uid, UploadUrlReq uploadUrlReq) {
        OssSceneEnum of = OssSceneEnum.of(uploadUrlReq.getScene());
        AssertUtil.isNotEmpty(of, "场景不存在");
        OssReq ossReq = OssReq.builder()
                .uid(uid)
                .fileName(uploadUrlReq.getFileName())
                .filePath(of.getPath())
                .build();
        return minIOTemplate.getPreSignedObjectUrl(ossReq);
    }
}
